#pragma once
namespace key
{
	template<class K>
	struct BSTreeNode
	{
		BSTreeNode<K>* _left;
		BSTreeNode<K>* _right;
		K _key;
		BSTreeNode(const K& key)
			: _left(nullptr)
			, _right(nullptr)
			, _key(key)
		{
		}
	};
	template<class K>
	class BSTree
	{
	public:
		typedef BSTreeNode<K> Node;
		BSTree() = default;


		BSTree(const BSTree<K>& t)
		{
			_root = Copy(t._root);
		}
		~BSTree()
		{
			Destory(_root);
			_root = nullptr;
		}
		BSTree<K>& operator=(BSTree<K> t)
		{
			swap(_root, t._root);
			return *this;
		}

		bool Insert(const K& key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}
			cur = new Node(key);
			if (parent->_key < key)
			{
				parent->_right = cur;
			}
			else parent->_left = cur;
			return true;
		}
		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}
		bool Find(const K& key)
		{
			if (_root == nullptr)
			{
				return false;
			}
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else
				{
					return true;
				}
			}
			return false;
		}
		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* parent = nullptr;
			if (cur == nullptr) return false;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							if (cur == parent->_left)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}

						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (cur == parent->_left)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}

						delete cur;
					}
					else
					{
						Node* MinRight = cur->_right;
						Node* pMinRight = cur;
						while (MinRight->_left)
						{
							pMinRight = MinRight;
							MinRight = MinRight->_left;
						}
						swap(cur->_key, MinRight->_key);
						if (pMinRight->_left == MinRight)
						{
							pMinRight->_left = MinRight->_right;
						}
						else
						{
							pMinRight->_right = MinRight->_right;

						}
						delete MinRight;
					}
					return true;
				}
			}
		}
	private:
		void _InOrder(Node* _root)
		{
			Node* cur = _root;
			if (cur == nullptr) return;
			_InOrder(cur->_left);
			cout << cur->_key << " ";
			_InOrder(cur->_right);
		}

		void Destory(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			if (root->_left) Destory(root->_left);
			if (root->_right) Destory(root->_right);
			delete root;
		}

		Node* Copy(Node* root)
		{
			if (root == nullptr) return nullptr;
			Node* copy = new Node(root->_key);
			copy->_left = Copy(copy->_left);
			copy->_right = Copy(copy->_right);
		}
	private:

		Node* _root = nullptr;
	};

}
namespace key_value
{
	template<class K,class V>
	struct BSTreeNode
	{
		BSTreeNode<K, V>* _left;
		BSTreeNode<K, V>* _right;
		K _key;
		V _value;
		BSTreeNode(const K& key, const V& value)
			: _left(nullptr)
			, _right(nullptr)
			, _key(key)
			, _value(value)
		{}
	};
	template<class K, class V>
	class BSTree
	{
	public:
		typedef BSTreeNode<K, V> Node;
		BSTree() = default;


		BSTree(const BSTree<K, V>& t)
		{
			_root = Copy(t._root);
		}
		~BSTree()
		{
			Destory(_root);
			_root = nullptr;
		}
		BSTree<K, V>& operator=(BSTree<K, V> t)
		{
			swap(_root, t._root);
			return *this;
		}

		bool Insert(const K& key, const V& value)
		{
			if (_root == nullptr)
			{
				_root = new Node(key, value);
				return true;
			}
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}
			cur = new Node(key, value);
			if (parent->_key < key)
			{
				parent->_right = cur;
			}
			else parent->_left = cur;
			return true;
		}
		void InOrder()
		{
			_InOrder(_root);
			cout << endl;
		}
		Node* Find(const K& key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else
				{
					return cur;
				}
			}
			return nullptr;
		}
		bool Erase(const K& key)
		{
			Node* cur = _root;
			Node* parent = nullptr;
			if (cur == nullptr) return false;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							if (cur == parent->_left)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}

						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (cur == parent->_left)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}

						delete cur;
					}
					else
					{
						Node* MinRight = cur->_right;
						Node* pMinRight = cur;
						while (MinRight->_left)
						{
							pMinRight = MinRight;
							MinRight = MinRight->_left;
						}
						swap(cur->_key, MinRight->_key);
						if (pMinRight->_left == MinRight)
						{
							pMinRight->_left = MinRight->_right;
						}
						else
						{
							pMinRight->_right = MinRight->_right;

						}
						delete MinRight;
					}
					return true;
				}
			}
		}
	private:
		void _InOrder(Node* _root)
		{
			Node* cur = _root;
			if (cur == nullptr) return;
			_InOrder(cur->_left);
			cout << cur->_key << " ";
			_InOrder(cur->_right);
		}

		void Destory(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			if (root->_left) Destory(root->_left);
			if (root->_right) Destory(root->_right);
			delete root;
		}

		Node* Copy(Node* root)
		{
			if (root == nullptr) return nullptr;
			Node* copy = new Node(root->_key);
			copy->_left = Copy(copy->_left);
			copy->_right = Copy(copy->_right);
		}
	private:

		Node* _root = nullptr;
	};

}

